package org.dromara.module.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.module.domain.BusinessCooperation;
import org.dromara.module.service.IBusinessCooperationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商务合作信息
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/cooperation")
public class BusinessCooperationController extends BaseController {

    private final IBusinessCooperationService businessCooperationService;

    /**
     * 查询商务合作信息列表
     */
    @SaCheckPermission("system:cooperation:list")
    @GetMapping("/list")
    public TableDataInfo<BusinessCooperation> list(BusinessCooperation businessCooperation, PageQuery pageQuery) {
        QueryWrapper<BusinessCooperation> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(businessCooperation.getPhone())){
            qw.like("phone", businessCooperation.getPhone());
        }
        qw.orderByDesc("create_time");

        TableDataInfo<BusinessCooperation> td = PageUtils.buildDataInfo(businessCooperationService.page(PageUtils.buildPage(),qw));
        List<BusinessCooperation> list = td.getRows();
        return td;
    }

    /**
     * 导出商务合作信息列表
     */
    @SaCheckPermission("system:cooperation:export")
    @Log(title = "商务合作信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusinessCooperation businessCooperation, HttpServletResponse response) {
        List<BusinessCooperation> list = businessCooperationService.list();
        ExcelUtil.exportExcel(list, "商务合作信息", BusinessCooperation.class, response);
    }

    /**
     * 获取商务合作信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:cooperation:query")
    @GetMapping("/{id}")
    public R<BusinessCooperation> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        BusinessCooperation businessCooperation = businessCooperationService.getById(id);
        if(businessCooperation != null){

        }
        return R.ok(businessCooperation);
    }

    /**
     * 新增商务合作信息
     */
    @SaCheckPermission("system:cooperation:add")
    @Log(title = "商务合作信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusinessCooperation businessCooperation) {
        return toAjax(businessCooperationService.save(businessCooperation));
    }

    /**
     * 修改商务合作信息
     */
    @SaCheckPermission("system:cooperation:edit")
    @Log(title = "商务合作信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusinessCooperation businessCooperation) {
        return toAjax(businessCooperationService.updateById(businessCooperation));
    }

    /**
     * 删除商务合作信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:cooperation:remove")
    @Log(title = "商务合作信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(businessCooperationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
